package cp3120.view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

import cp3120.model.User;
import cp3120.model.Password;

/**
 * The Registration form.
 *
 * @author Adam Scarr
 */
public class RegistrationUI extends JFrame {
    private JTextField username;
    private JPasswordField password;
    private JTextField firstName;
    private JTextField lastName;
    private JTextField homeAddress;
    private JTextField mailingAddress;
    private JTextField contactPhone;
    private JTextField contactEmail;
    private JButton register;
    private JButton cancel;
    private ActionListener listener = null;
    private JTextArea error;
    private int user_id;
    private Password oldPwd = null;

    /**
     * Copys data from the interface into a User model.
     * @param user the model to copy data into.
     */
    public void insertInto(User user) {
        user.setUsername(username.getText());

        if (oldPwd != null &&  password.getPassword().length == 0) {
            user.setPassword(oldPwd);
        } else {
            user.setPassword(new Password(new String(password.getPassword()), false));
        }
        user.setFirstName(firstName.getText());
        user.setLastName(lastName.getText());
        user.setHomeAddress(homeAddress.getText());
        user.setMailingAddress(mailingAddress.getText());
        user.setContactPhone(contactPhone.getText());
        user.setContactEmail(contactEmail.getText());
        user.setUserID(user_id);
    }

    /**
     * Populates the form with data from a user object.
     * @param user  The user to load from.
     */
    public void loadFrom(User user) {
        username.setText(user.getUsername());
        firstName.setText(user.getFirstName());
        lastName.setText((user.getLastName()));
        homeAddress.setText(user.getHomeAddress());
        mailingAddress.setText(user.getMailingAddress());
        contactPhone.setText(user.getContactPhone());
        contactEmail.setText(user.getContactEmail());
        user_id = user.getUserID();
        oldPwd = user.getPassword();
    }

    /**
     * Set the action listener to call when we press register.
     * @param al The action listener.
     */
    public void setRegistrationAction(ActionListener al) {
        if (listener != null) {
            register.removeActionListener(listener);
        }
        listener = al;
        register.addActionListener(al);
    }

    /**
     * Class constructor that sets up all internal components and layout.
     */
    public RegistrationUI() {
        username = new JTextField();
        password = new JPasswordField();
        firstName = new JTextField();
        lastName = new JTextField();
        homeAddress = new JTextField();
        mailingAddress = new JTextField();
        contactPhone = new JTextField();
        contactEmail = new JTextField();
        register = new JButton("Register");
        cancel = new JButton("Cancel");
        error = new JTextArea("");

        JTextArea description = new JTextArea("In order to register an account with us, please enter the following details.");
        description.setLineWrap(true);
        description.setColumns(20);
        description.setRows(3);
        description.setMaximumSize(new Dimension(100, 0));
        add(description, BorderLayout.NORTH);

        add(error, BorderLayout.SOUTH);

        JPanel userDetails = new JPanel();
        userDetails.setLayout(new GridLayout(9, 2));
        add(userDetails, BorderLayout.CENTER);

        userDetails.add(new JLabel("Username"));
        userDetails.add(username);
        userDetails.add(new JLabel("Password"));
        userDetails.add(password);
        userDetails.add(new JLabel("First Name"));
        userDetails.add(firstName);
        userDetails.add(new JLabel("Last Name"));
        userDetails.add(lastName);
        userDetails.add(new JLabel("Home Address"));
        userDetails.add(homeAddress);
        userDetails.add(new JLabel("Mailing Address"));
        userDetails.add(mailingAddress);
        userDetails.add(new JLabel("Phone"));
        userDetails.add(contactPhone);
        userDetails.add(new JLabel("Email"));
        userDetails.add(contactEmail);
        userDetails.add(register);
        userDetails.add(cancel);

        // Center the frame on the screen.
        pack();
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int centerX = (int)(screensize.getWidth() - getWidth()) / 2;
        int centerY = (int)(screensize.getHeight() - getHeight()) / 2;
        setLocation(centerX, centerY);

    }

    /**
     * Sets the error message
     * @param error the message to display.
     */
    public void setError(String error) {
        this.error.setText(error);
        pack();
    }
}

